"use strict"

let GamingLP_Claimable          = "0"
let GamingLP_RewardsSeconds     = "0"
let GamingLP_RewardTime         =  0

let balance_usdt   = 0
let liquidity_usdt = 0

function onLogin()
{
    onInitLiquidity();
}

async function onInitLiquidity()
{
    var contractUSDT     = GetContractERC20(Contract_USDT);
    var contractGamingLP = GetContractGamingLP();
    
    await RefreshRewardsSeconds();
    await RefreshAllow();

    balance_usdt = await contractUSDT.balanceOf(Helper.addressCur);
    balance_usdt = window._ethers.utils.formatUnits(balance_usdt, Contract_Decimals)
    balance_usdt = Helper.Fix2(balance_usdt)

    liquidity_usdt = await contractGamingLP.balanceOf(Helper.addressCur);
    liquidity_usdt = window._ethers.utils.formatUnits(liquidity_usdt, Contract_Decimals)
    liquidity_usdt = Helper.Fix2(liquidity_usdt)

    $("#balance_usdt").html("Balance: " + balance_usdt );
    $("#GLP_liquidity").html("Liquidity: " + liquidity_usdt );


    try{
        var rel  = await contractGamingLP["getfreeAmount(address)"](Helper.addressCur);
    }
    catch(e)
    {

    }

    GLP_ClaimableLoop();
    CheckApproveLoop();
}

async function RefreshRewardsSeconds()
{
    try{
        var contract = GetContractGamingLP();
        GamingLP_RewardsSeconds = await contract.GetRewardsSeconds(Helper.addressCur);
        GamingLP_RewardsSeconds = window._ethers.utils.formatUnits(GamingLP_RewardsSeconds, Contract_Decimals)
        GamingLP_RewardsSeconds  = Helper.Fix(GamingLP_RewardsSeconds)

        GamingLP_Claimable = await contract["GetRewards(address)"](Helper.addressCur);
        GamingLP_Claimable = window._ethers.utils.formatUnits(GamingLP_Claimable, Contract_Decimals)
        GamingLP_Claimable = Helper.Fix(GamingLP_Claimable)

        GamingLP_RewardTime = Date.now();
    }
    catch(e)
    {
    }
}

async function onclickApprove(id)
{
    if(id=="univ3_input_usdt"){
        var amount = $("#univ3_input_usdt").val();
        var status = await doApproveToGamingLP(Contract_USDT,amount,Contract_Decimals);
        if(status==1) {
            ShowToastr("Success","Approve USDT");
            allowUSDT = amount;
            return;
        }
    }
    setTimeout("RefreshAllow()", 3000);
    setTimeout("RefreshAllow()", 10000);
}

let allowUSDT = -1;

async function RefreshAllow()
{
    var contractUSDT    = GetContractERC20(Contract_USDT);
    if(allowUSDT==-1){
        allowUSDT = await contractUSDT.allowance(Helper.addressCur,Contract_GamingLP)
        allowUSDT = window._ethers.utils.formatUnits(allowUSDT, Contract_Decimals)
    }
}

async function CheckApproveLoop()
{
    while(true){
        if(Helper.addressCur!=null){

            var btn_Approve_USDT = false;

            var uu = $("#univ3_input_usdt").val();
            if(!isNaN(uu)&&uu!=""&&uu!="0"){
                if( Helper.isLessThanOrEqualTo(uu,allowUSDT) ){
                    $("#btn_Approve_USDT").addClass("d-none");
                    btn_Approve_USDT = false;
                }
                else {
                    $("#btn_Approve_USDT").removeClass("d-none");
                    btn_Approve_USDT = true;
                }
            }else{
                $("#btn_Approve_USDT").addClass("d-none");
                btn_Approve_USDT = false;
            }

            if(btn_Approve_USDT==false&&!isNaN(uu)&&uu!=""&&uu!="0"){
                $("#btn_Approve_Deposit").removeClass("disabled");
            }

        }
        await Helper.sleep(1000);
    }
}


async function GLP_ClaimableLoop(){
    while(true){
        var addValue =  Helper.Mul( GamingLP_RewardsSeconds,  ((Date.now() - GamingLP_RewardTime)/1000).toString()  )
        addValue = Helper.Add(GamingLP_Claimable , addValue );
        $("#GamingLP_Claimable").html( Helper.Fix8(addValue) );
        await Helper.sleep(100);
    }
}


async function onclickDeposit(){
    var input_amountUSDT = $("#univ3_input_usdt").val();

    $("#btn_Approve_Deposit").attr('disabled', true);    
    setTimeout("$('#btn_Approve_Deposit').attr('disabled', false);", 3000);

    var contract = GetContractGamingLP();
    const amountUDST = window._ethers.utils.parseUnits(input_amountUSDT, Contract_Decimals)

    console.log(amountUDST.toString(),Date.now() + 600000,Helper.addressCur)

    var tx  = await contract.addLiquidity(amountUDST, Date.now() + 600000 , Helper.addressCur ); // token0是USDT
    var rel = await tx.wait();
    if(rel.status==1){
        ShowToastr("Success","Add Liquidity");

        $("#univ3_input_cb").val("0");
        $("#univ3_input_usdt").val("0");

    }
    setTimeout("onInitLiquidity()", 3000);
    setTimeout("onInitLiquidity()", 10000);
    
}

async function onclickWithdrawAll(){
    $("#btn_Withdraw_All").attr('disabled', true);    
    setTimeout("$('#btn_Withdraw_All').attr('disabled', false);", 3000);

    var contract = GetContractGamingLP();
    var liquidity  = await contract.balanceOf(Helper.addressCur);
    if(liquidity.toString()=="0") return;

    var tx  = await contract.removeLiquidity(liquidity,Date.now() + 3600000);
    var rel = await tx.wait();
    if(rel.status==1){
        ShowToastr("Success","Withdraw All");
    }
    setTimeout("onInitLiquidity()", 3000);
    setTimeout("onInitLiquidity()", 10000); 

}

async function onclickWithdraw(){
    $("#btn_Withdraw").attr('disabled', true);    
    setTimeout("$('#btn_Withdraw').attr('disabled', false);", 3000);

    var contract = GetContractGamingLP();
    var input_liquidity = $("#input_liquidity").val();
    if(input_liquidity.toString()=="0") return;

    var tx  = await contract.removeLiquidity(input_liquidity,Date.now() + 3600000);
    var rel = await tx.wait();
    if(rel.status==1){
        ShowToastr("Success","Withdraw All");
    }
    setTimeout("onInitLiquidity()", 3000);
    setTimeout("onInitLiquidity()", 10000); 

}

async function onclickClaim(){
    
    var contractGamingLP = GetContractGamingLP();
    var tx  = await contractGamingLP.Claim(Helper.addressCur);
    var rel = await tx.wait();
    if(rel.status==1){
        ShowToastr("Success","Claim All");
    }
    setTimeout("RefreshRewardsSeconds()", 3000);
    setTimeout("RefreshRewardsSeconds()", 10000);

}


(function($) {
    "use strict"



})(jQuery);


